package com.hg.controller;

import com.github.pagehelper.PageInfo;
import com.hg.common.dto.DeptDTO;
import com.hg.common.dto.PageDTO;
import com.hg.common.enums.ResultEnum;
import com.hg.common.exception.ParamException;
import com.hg.common.vo.DeptVO;
import com.hg.common.vo.ResultVO;
import com.hg.entity.Dept;
import com.hg.service.IDeptService;
import com.hg.service.impl.DeptServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门
 * Created by skh on 2018/1/10
 */
@RestController
@Slf4j
@RequestMapping("/dept")
public class DeptController {

	@Autowired
	private IDeptService deptService;

	@PostMapping("/save")
	public ResultVO save(@Validated DeptVO deptVO) {
		deptService.save(deptVO);
		return ResultVO.success();
	}

	@PostMapping("/update")
	public ResultVO update(@Validated DeptVO deptVO) {
		deptService.update(deptVO);
		return ResultVO.success();
	}

	@PostMapping("/delete")
	public ResultVO delete(Integer deptId) {
		if (deptId == null) {
			throw new ParamException(ResultEnum.ID_IS_NULL);
		}
		deptService.delete(deptId);
		return ResultVO.success();
	}

	@GetMapping("/list")
	public ResultVO list(){
		List<Dept> deptList = deptService.selectAll();
		return ResultVO.success(deptList);
	}

	@GetMapping("/page")
	public ResultVO listByPage(@Validated PageDTO pageDTO){
		PageInfo<Dept> deptList = deptService.selectAllByPage(pageDTO);
		return ResultVO.success(deptList);
	}

	@GetMapping("/tree")
	public ResultVO tree() {
		List<DeptDTO> deptDTOList = deptService.tree();
		return ResultVO.success(deptDTOList);
	}
}
